package de.hsb.ai.agent.analyzer;

import game.Move;

import java.util.Collections;
import java.util.List;

import de.hsb.ai.domain.GameStarted;
import de.hsb.ai.util.map.ScotlandJadeMapHelper.ScotlandJadeVertex;

/**
 * A AnalyzerSlaveDetective is controlled by a AnalyzerMasterDetective
 * and just follows the orders it's given by him. 
 */
@SuppressWarnings("serial")
public class AnalyzerSlaveDetective extends AbstractAnalyzerDetective {
	
	private int targetIndex = -1;
	
	public AnalyzerSlaveDetective() {
		super("analyzer-slave");
	}
	
	@Override
	protected void handleGameStarted(GameStarted g) {
		super.handleGameStarted(g);
		
		targetIndex = -1;
		pathFinder = new PathFinder(map);
	}
	
	@Override
	protected void handleDetectiveMove(ScotlandJadeVertex targetVertex, int transport, boolean ownMove) {
		super.handleDetectiveMove(targetVertex, transport, ownMove);
		if (ownMove && targetVertex.getIndex() == targetIndex) {
			for (AbstractAnalyzerDetective partner : partners) partner.onParnerReachedVertex(this, targetVertex);
		}
	}

	@Override
	protected Move getMove(List<Move> options) {
		Move nextMove = super.getMove(options);
		if (nextMove == null) {
			nextMove = getMoveTowards(options, targetIndex);
		}
		if (nextMove == null) {
			logger.warn("LAST RESORT: RANDOM MOVE <----------------------------------------------");
			Collections.shuffle(options);
			nextMove = options.get(0);
		}
		return nextMove;
	}
	
	void setTarget(int targetIndex) {
		logger.info("new target: " + targetIndex);
		this.targetIndex = targetIndex;
	}

}
